﻿using ChineseAreaService;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
using Sino.CapacityCloud.Repositories.Models;
using Sino.CapacityCloud.Services;
using Sino.CapacityCloud.WebApi.ViewModels;
using Sino.WebApi.Framework.Core.Filters;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;

namespace Sino.CapacityCloud.WebApi.ApiControllers
{

    [SupressTokenValidation()]
    public class CommonController
        : ApiController<CommonController>
    {

        private readonly IRandomPasswordService _randomPasswordService;
        private readonly IChinaAreaService _areaService;

        public CommonController(
            IHostingEnvironment env,
            IApplicationEnvironment applicationEnvironment,
            IDistributedCache cache,
            IChinaAreaService areaService,
            ITokenGenertationService tokenGenertationService,
            ApplicationDbContext dbContext,
            ApplicationUserManager userManager,
            ApplicationRoleManager roleManager,
            ApplicationSignInManager signInManager,
            ILoggerFactory loggerFactory, IRandomPasswordService randomPasswordService)
            : base(env, applicationEnvironment, cache, tokenGenertationService, dbContext, userManager, roleManager, signInManager, loggerFactory)
        {
            this._areaService = areaService;
            this._randomPasswordService = randomPasswordService;
        }

        [SupressTokenValidation]
        [SupressUserLogin]
        public async Task<JsonResult> SayHello(string name)
        {
            return await Task.FromResult(Json(new { message = $"Hello, {name}!" }));
        }

        public async Task<JsonResult> GetProvince()
        {
            var result = new Dictionary<string, string>();

            var names = this._areaService.ProvinceNames;
            var codes = this._areaService.ProvinceCodes;

            for (int i = 0; i < names.Count; i++)
            {
                result.Add(codes[i], names[i]);
            }

            return await this.PackageResultAsync(new AreaResponse()
            {
                List = result.Select(x => new AreaResponse.AreaItem() { Name = x.Value, Value = x.Key }).ToList()
            });
        }

        public async Task<JsonResult> GetCity(string provincecode)
        {
            var result = new Dictionary<string, string>();

            var codes = this._areaService.CityCodeInProvince(provincecode);
            var names = this._areaService.CityNameInProvince(this._areaService.GetAreaName(provincecode));

            for (int i = 0; i < names.Count; i++)
            {
                if (names[i] != null && codes[i] != null && codes[i] != null)
                {
                    result.Add(codes[i], names[i]);
                }
            }

            return await this.PackageResultAsync(new AreaResponse()
            {
                List = result.Select(x => new AreaResponse.AreaItem() { Name = x.Value, Value = x.Key }).ToList()
            });
        }

        public async Task<JsonResult> GetCounty(string citycode)
        {
            var result = new Dictionary<string, string>();

            var codes = this._areaService.CountyCodeInCity(citycode);
            var names = this._areaService.CountyNameInCity(this._areaService.GetAreaName(citycode, false));

            for (int i = 0; i < names.Count; i++)
            {
                if (names[i] != null && codes[i] != null && codes[i] != null)
                {
                    result.Add(codes[i], names[i]);
                }
            }

            return await this.PackageResultAsync(new AreaResponse()
            {
                List = result.Select(x => new AreaResponse.AreaItem() { Name = x.Value, Value = x.Key }).ToList()
            });
        }

    }

}